const Router = require('koa-router');
const router = new Router();

const UserModel = require('../../model/user');

router.prefix('/user');
router
    .get('/', function (ctx, next) {
        ctx.body = 'Hello World!';
    })
    .get('/:id', async (ctx, next) => {
        ctx.body = await UserModel.findOne({
            username: 'zhuchuanpeng'
        }).exec();

        // UserModel.findOne({username: 'test'})
        //     .then(function (doc) {
        //         "use strict";
        //         console.log(doc);
        //         ctx.body = doc;
        //     })
    })
    .post('/', function (ctx, next) {
        // ...
    })
    .put('/:id', async (ctx, next) => {
        // ...
        const zhu = new UserModel({
            username: 'zhuchuanpeng',
            password: 'aaa123',
            phone: '15502622805'
        });
        console.log('save', zhu);
        const x = await zhu.save();
        console.log('x', x);
        ctx.body = 'success';
    })
    .del('/:id', function (ctx, next) {
        // ...
    })
    .all('/:id', function (ctx, next) {
        // ...
    });

module.exports = router;
